package com.itouch.bracelet.diseaseinfo;

import io.rong.imkit.RongIM;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import android.content.Intent;
import android.os.AsyncTask;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.itouch.bracelet.R;
import com.itouch.bracelet.activity.BaseActivity;
import com.itouch.bracelet.bean.DiseaseInfo;
import com.itouch.bracelet.bean.LoginUser;
import com.itouch.bracelet.bean.RGroup;
import com.itouch.bracelet.cache.UserCache;
import com.itouch.bracelet.result.DiseaseInfoArrayResult;
import com.itouch.util.JsonAndPhpUtil;

public class DiseaseInfoActivity extends BaseActivity implements
		View.OnClickListener {

	ListView listView;
	TextView btn_chat;
	LinearLayout tv_search;
	TextView tv_title;

	int id = -1;
	String lebel = "标题";
	DiseaseInfoAdapter mAdapter;
	DiseaseInfoActivity mThis;
	ArrayList<DiseaseInfo> mData;

	@Override
	protected int setContentViewById() {
		// TODO Auto-generated method stub
		mThis = this;
		return R.layout.activity_diseaseinfo;
	}

	@Override
	protected void initViews() {
		// TODO Auto-generated method stub
		listView = (ListView) findViewById(R.id.list);

//		btn_back = (Button) findViewById(R.id.back);
		btn_chat = (TextView) findViewById(R.id.chat);
//		btn_back.setOnClickListener(this);
		btn_chat.setOnClickListener(this);

		tv_search = (LinearLayout) findViewById(R.id.search);
		tv_search.setOnClickListener(this);
		
		tv_title = (TextView) findViewById(R.id.title);

		mAdapter = new DiseaseInfoAdapter(mThis, mData);
		listView.setAdapter(mAdapter);

		listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				Log.e("gaga", "arg2:" + arg2);
				Intent intent = new Intent(mThis, DiseaseInfoDetailListActivity.class);
				intent.putExtra("plateid", mData.get(arg2).getId());
				intent.putExtra("plate", mData.get(arg2).getName());
				intent.putExtra("label", lebel);
				startActivity(intent);
			}
		});

	}

	@Override
	protected void initData() {
		// TODO Auto-generated method stub
//		initImageLoader();
		Intent intent = getIntent();
		id = intent.getIntExtra("type", -1);
		lebel = intent.getStringExtra("label");
		tv_title.setText(lebel);
		Log.e("test", "typeid:" + id);
		new GetDiseaseInfos().execute();
	}

	public class GetDiseaseInfos extends AsyncTask<String, String, String> {
		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			show();
		}

		@Override
		protected String doInBackground(String... params) {
			List<NameValuePair> lists = new ArrayList<NameValuePair>();
			String url = Constants.URL + Constants.DISEASEINFO_MAIN;
			lists.add(new BasicNameValuePair("parentId", id + ""));

			try {
				String ss = JsonAndPhpUtil.postHttpData(url, lists,
						DiseaseInfoActivity.this);
				Log.e("test", "ss_ss:" + ss);
				return ss;
			} catch (Exception e) {
				e.printStackTrace();
			}
			return null;
		}

		@Override
		protected void onPostExecute(String result) {
			super.onPostExecute(result);
			disMiss();
			if (result == null || result.length() < 1) {
				return;
			}
			DiseaseInfoArrayResult a = null;
			try {
				a = new Gson().fromJson(result, DiseaseInfoArrayResult.class);
			} catch (JsonSyntaxException e) {
				// TODO: handle exception
				Toast.makeText(mThis, "服务器数据异常！", Toast.LENGTH_SHORT).show();
			}

			if (a == null) {
				return;
			}
			if (a.isFlag()) {
				mData = a.getRows();
				mAdapter.refrashDataAndView(mData);
			} else {
				Toast.makeText(context, a.getMsg() + "", Toast.LENGTH_LONG)
						.show();
			}

		}

	}

	@Override
	public void onClick(View arg0) {
		// TODO Auto-generated method stub
		switch (arg0.getId()) {
		case R.id.back:
			finish();
			break;

		case R.id.chat:
			String title=tv_title.getText().toString();
			LoginUser user =UserCache.getOauth(context);
			if(user==null){
				return ;
			}
			ArrayList<RGroup> list = user.getRongyungroup();
			if(list!=null&&list.size()>0){
				
				for(int i =0;i<list.size();i++){
					RGroup rGroup=list.get(i);
					if(rGroup.getGroupName().equals(title)){
						RongIM.getInstance().startGroupChat(this, rGroup.getId(),
								rGroup.getGroupName().toString());
						
						break;						
					}
				}
			}
						break;

		case R.id.search:
			Intent intent = new Intent(mThis, SearchActivity.class);
			startActivity(intent);
			break;
		default:
			break;
		}
	}

//	public void initImageLoader() {
//		ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(
//				context).threadPriority(Thread.NORM_PRIORITY - 2) // 设置线程的优先级
//				.denyCacheImageMultipleSizesInMemory() // 当同一个Uri获取不同大小的图片，缓存到内存时，只缓存一个。默认会缓存多个不同的大小的相同图片
//				.discCacheFileNameGenerator(new Md5FileNameGenerator()) // 设置文件缓存的名字
//																		// Md5FileNameGenerator()：通过Md5将url生产文件的唯一名字
//				.build();
//		ImageLoader.getInstance().init(config);
//
//	}

}
